package com.imooc.service.impl;

import com.imooc.BO.center.OrderItemsCommentBO;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.*;
import com.imooc.pojo.OrderItems;
import com.imooc.pojo.OrderStatus;
import com.imooc.pojo.Orders;
import com.imooc.service.MyCommentsService;
import com.imooc.vo.MyCommentVO;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.annotation.Order;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author gcq
 * @Create 2022-01-08
 */
@Service
public class MyCommentsServiceImpl implements MyCommentsService {


    @Autowired
    OrderItemsMapper orderItemsMapper;
    @Autowired
    ItemsCommentsMapperCustomer itemsCommentsMapperCustomer;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrderStatusMapper orderStatusMapper;

    @Autowired
    Sid sid;

    @Override
    public List<OrderItems> queryPendingComment(String orderId) {
        OrderItems orderItems = new OrderItems();
        orderItems.setOrderId(orderId);
        return orderItemsMapper.select(orderItems);
    }

    @Override
    public void saveComments(String userId, String orderId, List<OrderItemsCommentBO> commentBOList) {
        // 1、保存评价 items_comment
        for (OrderItemsCommentBO oic : commentBOList) {
            oic.setCommentId(sid.nextShort());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("commentList", commentBOList);
        itemsCommentsMapperCustomer.saveComments(map);

        // 2、修改订单表已评价 orders

        Orders orders = new Orders();
        orders.setId(orderId);
        orders.setIsComment(YesOrNo.YES.type);
        ordersMapper.updateByPrimaryKeySelective(orders);

        // 3、修改订单状态表的留言时间 orders_status
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(orderId);
        orderStatus.setCommentTime(new Date());
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
    }

    @Override
    public List<MyCommentVO> queryMyComments(String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        return itemsCommentsMapperCustomer.queryMyComments(map);
    }
}